this.name        = "buzzer-wowbagger-insult";
this.author      = "Darren Salt";
this.copyright   = "© 2009 Darren Salt";
this.description = "Handles Wowbagger's insults";
this.version     = "1.0";

this.insulted = false;

this.insult = function ()
{
  // Between 4s and 6s before the player is insulted
  this.timer = new Timer (this, this.do_insult, 4 + 2 * Math.random ());
  this.timer.start();
};

this.do_insult = function ()
{
  if (!this.insulted)
  {
    this.ship.displayName = 'Annoying Little Buzzer'; // :-)

    // Insult the player
    var x = Math.random ();
    if (x < 0.25)
      this.ship.commsMessage ("Commander [commander_name], hello. [wowbagger-leadin] [wowbagger-adjective] [wowbagger-name][wowbagger-leadout]");
    else if (x < 0.5)
      this.ship.commsMessage ("Hello, Commander [commander_name]. [wowbagger-leadin] [wowbagger-adjective] [wowbagger-name][wowbagger-leadout]");
    else if (x < 0.75)
      this.ship.commsMessage ("Greetings, Commander [commander_name]. [wowbagger-leadin] [wowbagger-adjective] [wowbagger-name][wowbagger-leadout]");
    else
      this.ship.commsMessage ("Welcome to [system_name], Commander [commander_name]. [wowbagger-leadin] [wowbagger-adjective] [wowbagger-name][wowbagger-leadout]");
  }

  // 25% chance of hyperspacing away
  if (Math.random () < 0.25)
  {
	if(this.timer) {
		this.timer.stop();
		delete this.timer;
	}
    this.timer = new Timer(this, this.done_insult, 4 + 2 * Math.random ()); 
    this.timer.start();
  }
  else // Maybe forget that an insult was issued
    this.maybe_forget_insulted ();
};

this.playerWillEnterWitchspace = function() {
    if (this.timer) {
		this.timer.stop();
		delete this.timer;
	}
};

this.done_insult = function ()
{
  this.ship.AIState = "INSULTED";
};

this.maybe_forget_insulted = function ()
{
  // 10% chance of forgetting that an insult has been issued
  this.insulted = (Math.random () < 0.9);
  if (this.ship.AIState == "INSULTED")
    this.ship.exitAI ();
};
